﻿Shader "Unlit/BasicLightingPerFragment"
{
    Properties
    {
        _MainTex ("Main Texture", 2D) = "white" {}

        [Header(Ambient)]
        _Ambient("Intensity", Range(0.0,1.0)) = 0.1
        _AmbColor("Color", Color) = (1.0, 1.0, 1.0, 1.0)

        [Header(Diffuse)]
        _Diffuse("Val",Range(0.0, 1.0)) = 1.0
        _DifColor("Color", Color) = (1.0, 1.0, 1.0, 1.0)

        [Header(Specular)]
        [Toggle] _Spec("Enabled?", float) = 0.0
        _Shininess("Shininess", Range(0.1, 10)) = 1.0
        _SpecColor("Specular Color", Color) = (1.0, 1.0, 1.0, 1.0)

        [Header(Emission)]
        _EmissionTex("Emission Texture",2D) = "gray" {}
        _EmiVal("Intensity",float) = 0.0
        [HDR] _EmiColor("Color", Color) = (1.0, 1.0, 1.0, 1.0)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "Queue"="Geomotry" "LightMode"="ForwardBase" }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            // Change "shader_feature" with "pragma_compile" if you want set this keyword from C# code
            #pragma shader_feature __ _SPEC_ON

            #include "UnityCG.cginc"

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 worldPos : TEXCOORD1;
                float3 worldNormal : TEXCOORD2;
            };

            v2f vert (appdata_base v)
            {
                v2f o;

                //World position
                o.worldPos = mul(unity_ObjectToWorld, v.vertex);

                //Clip position
                o.vertex = UnityObjectToClipPos(v.vertex);

                //Normal in WorldSpace
                o.worldNormal = normalize(UnityObjectToWorldNormal(v.normal));

                o.uv = v.texcoord;

                return o;
            }
            
            sampler2D _MainTex;
            float4 _MainTex_ST;

            fixed4 _LightColor0;

            //Diffuse
            fixed _Diffuse;
            fixed4 _DifColor;

            //Specular
            fixed _Shininess;
            fixed4 _SpecColor;

            //Ambient
            fixed _Ambient;
            fixed4 _AmbColor;

            //Emission
            sampler2D _EmissionTex;
            fixed4 _EmiColor;
            fixed _EmiVal;

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 c = tex2D(_MainTex, i.uv);

                //Light direction
                float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);

                //Camera direction
                float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);

                float3 worldNormal = normalize(i.worldNormal);

                //Compute ambient lighting
                fixed4 amb = _Ambient * _AmbColor;

                //Compute the diffuse lighting
                fixed NdotL = max(0.0,dot(worldNormal,lightDir)*_LightColor0);
                fixed4 dif = NdotL*_Diffuse*_LightColor0*_DifColor;

                fixed4 light = dif+amb;

                //Compute the specular lighting
                #if _SPEC_ON
                float3 refl = normalize(reflect(-lightDir,worldNormal));
                float RdotV = max(0.0,dot(refl,viewDir));
                fixed4 spec = pow(RdotV,_Shininess)*_LightColor0*ceil(NdotL)*_SpecColor；

                light+=spec;
                #endif

                c.rgb *= light.rgb;

                //Compute Emission
                fixed4 emi = tex2D(_EmissionTex,i.uv).r*_EmiColor*_EmiVal;
                c.rgb+=emi.rgb;

                return c;
            }
            ENDCG
        }
    }
}
